package ServerService;

import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;
import java.util.Properties;

import common.Message;
import common.MessageType;
import common.user;

public class registerSer extends Thread {
    public static boolean loop = true;
    private static Properties properties = connectSer.getProperties();
    private static Hashtable<String, user> validUser = connectSer.getValidUser();
    ServerSocket Sersocket = null;

    /**
     * 服务端接授注册请求方法
     */
    @Override
    public void run() {
        while (loop) {
            try {

                Sersocket = new ServerSocket(11123);
                System.out.println("\n注册端口在" + Sersocket.getLocalPort() + "开启");
                Socket socket2 = Sersocket.accept();

                ObjectInputStream ois = new ObjectInputStream(socket2.getInputStream());
                ObjectOutputStream oos = new ObjectOutputStream(socket2.getOutputStream());

                user u = (user) ois.readObject();

                // 密码与用户名判断
                for (String key : properties.stringPropertyNames()) {
                    if (key.equals(u.getUser())) {
                        Message mess = new Message();
                        mess.setMessType(MessageType.MESSAGE_USER_REGISTER_FAIL_1);
                        mess.setContent("Duplicate_username");
                        oos.writeObject(mess);

                        continue;
                    }
                }
                boolean isDigit = false;
                boolean isLetter = false;
                for (int i = 0; i < u.getPasswd().length(); i++) {
                    if (Character.isDigit(u.getPasswd().charAt(i))) { // 用char包装类中的判断数字的方法判断每一个字符
                        isDigit = true;
                    } else if (Character.isLetter(u.getPasswd().charAt(i))) { // 用char包装类中的判断字母的方法判断每一个字符
                        isLetter = true;
                    }
                }
                String regex = "^[a-zA-Z0-9!-/]{6,12}$";
                if (!(isDigit && isLetter && u.getPasswd().matches(regex))) {
                    Message mess = new Message();
                    mess.setMessType(MessageType.MESSAGE_USER_REGISTER_FAIL_2);
                    mess.setContent("Password must contain numbers and letters Not less than 6 digits");
                    oos.writeObject(mess);
                    continue;
                }

                // 存入DB
                String path = "src\\userlist\\ulist.properties";
                properties.setProperty(u.getUser(), u.getPasswd());
                properties.store(new FileWriter(path), "userList");

                // 加载到内存(hashtable),发送信息到客户端
                if (validUser.put(u.getUser(), u) == null) {// hashtable等于null表示成功
                    Message Remessage = new Message();
                    Remessage.setMessType(MessageType.MESSAGE_USER_REGISTER_SUCCESS);

                    oos.writeObject(Remessage);

                } else {
                    System.out.println("用户:" + u.getUser() + " 注册失败");
                    Message Remessage = new Message();
                    Remessage.setMessType(MessageType.MESSAGE_USER_REGISTER_FAIL);

                    oos.writeObject(Remessage);

                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    Sersocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public registerSer() {

    }

    /**
     * 将ulist中所有用户信息写入hashtable
     */
    public static void writeDB() {
        if (properties.stringPropertyNames() != null) {
            for (String key : properties.stringPropertyNames()) {

                user u = new user(key, properties.getProperty(key));
                validUser.put(key, u);
            }
        } else {
            return;
        }

    }
}
